// pages/2411Ayiliao-pages/nucleic/report.js
Page({
	data: {
		reportData: {
			name: '蒋峰',
			idNumber: '378909199870098978',
			age: '24岁',
			department: '保健科',
			doctor: '王静',
			sampleType: '鼻咽拭子',
			sampleNumber: '123476',
			testMethod: '实时荧光定量 (PCR) 技术',
			samplingDate: '2020-09-09 10:00',
			testItem: '新型冠状病毒核酸检测',
			result: '阴性',
			resultClass: 'negative',
			reportDate: '2020-09-09',
			inspector: '王静',
			reviewer: '李凤'
		}
	},

	onLoad(options) {
		console.log('核酸报告页面加载，参数:', options)
		if (options.id) {
			this.loadReportData(options.id)
		}
	},

	// 加载报告数据
	loadReportData(reportId) {
		wx.showLoading({
			title: '加载中...'
		})

		wx.request({
			url: `http://localhost:8080/nucleic/report/${reportId}`,
			method: 'GET',
			header: {
				'Content-Type': 'application/json'
			},
			success: (res) => {
				wx.hideLoading()

				if (res.statusCode === 200 && res.data) {
					let reportData = res.data

					// 处理数据格式
					if (typeof reportData === 'string') {
						try {
							reportData = JSON.parse(reportData)
						} catch (e) {
							console.error('JSON解析失败:', e)
							return
						}
					}

					// 检查数据是否在嵌套对象中
					if (reportData.data && typeof reportData.data === 'object') {
						reportData = reportData.data
					}

					// 处理报告数据
					const processedData = {
						name: reportData.name || '蒋峰',
						idNumber: reportData.idNumber || '378909199870098978',
						age: reportData.age || '24岁',
						department: reportData.department || '保健科',
						doctor: reportData.doctor || '王静',
						sampleType: reportData.sampleType || '鼻咽拭子',
						sampleNumber: reportData.sampleNumber || '123476',
						testMethod: reportData.testMethod || '实时荧光定量 (PCR) 技术',
						samplingDate: reportData.samplingDate || '2020-09-09 10:00',
						testItem: reportData.testItem || '新型冠状病毒核酸检测',
						result: reportData.result || '阴性',
						resultClass: this.getResultClass(reportData.result),
						reportDate: reportData.reportDate || '2020-09-09',
						inspector: reportData.inspector || '王静',
						reviewer: reportData.reviewer || '李凤'
					}

					this.setData({
						reportData: processedData
					})

					wx.showToast({
						title: '报告加载成功',
						icon: 'success'
					})
				} else {
					console.error('加载报告失败:', res.statusCode)
					wx.showToast({
						title: '报告加载失败',
						icon: 'none'
					})
				}
			},
			fail: (error) => {
				wx.hideLoading()
				console.error('网络请求失败:', error)
				wx.showToast({
					title: '网络请求失败',
					icon: 'none'
				})
			}
		})
	},

	// 获取结果样式类
	getResultClass(result) {
		if (result === '阴性') {
			return 'negative'
		} else if (result === '阳性') {
			return 'positive'
		} else {
			return 'unknown'
		}
	}
}) 